using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Home_AllocateEducators : System.Web.UI.Page
{
    #region Helper Functions
    protected string GetCampusName(string sPlacementTypeId)
    {
        PlacementManager.PlacementTypeComponent oPlacementTypeC = new PlacementManager.PlacementTypeComponent();
        int campusId = oPlacementTypeC.SelectOne(Int32.Parse(sPlacementTypeId)).CampusId;

        PlacementManager.CampusComponent oCampusC = new PlacementManager.CampusComponent();
        return oCampusC.SelectOne(campusId).CampusCode;

    }
    protected string GetUnitName(string unitId)
    {
        PlacementManager.UnitComponent oUnitC = new PlacementManager.UnitComponent();
        return oUnitC.SelectOne(Int32.Parse(unitId)).UnitCode + " - " + oUnitC.SelectOne(Int32.Parse(unitId)).UnitName;
    }
    protected string GetCategoryName(string categoryId)
    {
        PlacementManager.CategoryComponent oCategoryC = new PlacementManager.CategoryComponent();
        return oCategoryC.SelectOne(Int32.Parse(categoryId)).CategoryName;
    }
    
    protected string GetVenueDetails(string sVenuePlacementTypeId)
    {
        //Get the varaiables for this venue for this placement type
        int venuePlacementTypeId = Int32.Parse(sVenuePlacementTypeId);
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(venuePlacementTypeId);
        int maxStudents = oLvpt.MaxNumStudents;
        bool isRestricted = oLvpt.IsRestricted;
        int venueId = oLvpt.VenueId;

        //Get the venue name
        PlacementManager.VenueComponent oVenueC = new PlacementManager.VenueComponent();
        string venueName = oVenueC.SelectOne(venueId).VenueName;
        if (venueName.Length > 30)
        {
            venueName = venueName.Substring(0, 31) + "...";
        }

        string sToReturn = "<span style=\"font-weight:bold;\"><a href='Venues.aspx?id=" + venueId + "'>" + venueName + "</a></span>";
        
        return sToReturn;
    }
    protected string GetEducatorDetails(string sContactId)
    {
        PlacementManager.ContactComponent oContactC = new PlacementManager.ContactComponent();
        return "<a href='Contacts.aspx?id=" + oContactC.SelectOne(Int32.Parse(sContactId)).ContactId.ToString() + "'>" + oContactC.SelectOne(Int32.Parse(sContactId)).LastName + ", " + oContactC.SelectOne(Int32.Parse(sContactId)).FirstName + "</a>";
    }
    #endregion


    protected void gvPlacementTypes_DataBound(object sender, EventArgs e)
    {
        this.gvAvailableEducators.DataBind();
        this.gvVenues.DataBind();
    }
    protected void gvPlacementTypes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow gvr = e.Row;

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string toolTip = this.GetUnitName(DataBinder.Eval(e.Row.DataItem, "UnitId").ToString());
            toolTip += "<br /> Semester:" + DataBinder.Eval(e.Row.DataItem, "SemesterId").ToString();
            toolTip += "<br /> Year:" + DataBinder.Eval(e.Row.DataItem, "Year").ToString();
            toolTip += "<br /> Campus:" + this.GetCampusName(DataBinder.Eval(e.Row.DataItem, "PlacementTypeId").ToString());
            toolTip += "<br /> Category:" + this.GetCategoryName(DataBinder.Eval(e.Row.DataItem, "CategoryId").ToString());
            toolTip += "<br /> Start Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateStart"))).ToShortDateString();
            toolTip += "<br /> End Date:" + (Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "DateEnd"))).ToShortDateString();


            gvr.Cells[1].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[1].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[2].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[2].Attributes.Add("onmouseout", "hideToolTip();");
            gvr.Cells[3].Attributes.Add("onmouseover", "showToolTip(event,'" + toolTip + "');");
            gvr.Cells[3].Attributes.Add("onmouseout", "hideToolTip();");



        }
    }
    protected void gvPlacementTypes_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvVenues.SelectedIndex = 0;
    }



    protected void gvVenues_DataBound(object sender, EventArgs e)
    {
        //If there are no venues allocated then we want to dsiable the add eductors button
        //and add students button
        if (this.gvVenues.Rows.Count > 0)
        {

            this.btnAddCheckedEducators.Enabled = true;

        }
        else
        {

            this.btnAddCheckedEducators.Enabled = false;

        }
    }
    protected void gvVenues_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvVenues_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        PlacementManager.LinkVenuePlacementTypeComponent oLvptC = new PlacementManager.LinkVenuePlacementTypeComponent();
        PlacementManager.LinkVenuePlacementType oLvpt = oLvptC.SelectOne(Int32.Parse(this.gvVenues.DataKeys[e.RowIndex].Value.ToString()));

        e.NewValues.Add("VenueId", oLvpt.VenueId);
        e.NewValues.Add("PlacementTypeId", oLvpt.PlacementTypeId);
        e.NewValues.Add("IsActive", oLvpt.IsActive);
        e.NewValues.Add("CreatedBy", oLvpt.CreatedBy);
        e.NewValues.Add("DateCreated", oLvpt.DateCreated);
        e.NewValues.Add("DateUpdated", DateTime.Now);
        e.NewValues.Add("UpdatedBy", User.Identity.Name);
    }
    protected void gvVenues_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {
        e.KeepInEditMode = true;
    }

    protected void gvVenues_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button btnRemoveCheckedStudents = (Button)e.Row.FindControl("btnRemoveCheckedEducators");
            GridView gvAllocatedEducators = (GridView)e.Row.FindControl("gvAllocatedEducators");


            if (gvAllocatedEducators != null)
            {
                gvAllocatedEducators.DataBind();
                if (gvAllocatedEducators.Rows.Count == 0)
                {
                    btnRemoveCheckedStudents.Visible = false;
                }
            }
        }
    }
    protected void gvVenues_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void gvVenues_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void gvAvailableStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.AllocateCheckedEducators();
    }

    protected void AllocateCheckedEducators()
    {
        if (this.gvVenues.SelectedIndex > -1)
        {
            PlacementManager.EducatorComponent oEducatorC = new PlacementManager.EducatorComponent();
            int index = 0;
            int contactId = 0;

            foreach (GridViewRow gvr in this.gvAvailableEducators.Rows)
            {
                if (gvr.RowType == DataControlRowType.DataRow)
                {
                    CheckBox RowLevelCheckBox = (CheckBox)gvr.FindControl("RowLevelCheckBox");
                    if (RowLevelCheckBox.Checked)
                    {
                        contactId = Int32.Parse(this.gvAvailableEducators.DataKeys[index].Value.ToString());
                        PlacementManager.Educator oEducator = new PlacementManager.Educator();

                        oEducator.CreatedBy = User.Identity.Name;
                        oEducator.DateCreated = DateTime.Now;
                        oEducator.DateUpdated = DateTime.Now;
                        oEducator.UpdatedBy = User.Identity.Name;
                        oEducator.IsActive = true;
                        oEducator.ContactId = contactId;
                        oEducator.VenuePlacementTypeId = Int32.Parse(this.gvVenues.SelectedDataKey.Value.ToString());
                        oEducatorC.Insert(oEducator);

                    }
                }
                index++;
            }

            this.gvAvailableEducators.DataBind();
            this.gvVenues.DataBind();
        }
    }
    protected void btnAddCheckedEducators_Click(object sender, EventArgs e)
    {
        this.AllocateCheckedEducators();
    }
    protected void btnRemoveCheckedEducators_Click(object sender, EventArgs e)
    {
        PlacementManager.EducatorComponent oEducatorC = new PlacementManager.EducatorComponent();
        int index = 0;
        int contactId = 0;
        foreach (GridViewRow gvrVenues in this.gvVenues.Rows)
        {
            if (gvrVenues.RowType == DataControlRowType.DataRow)
            {
                GridView gvAllocatedEducators = (GridView)gvrVenues.FindControl("gvAllocatedEducators");
                if (gvAllocatedEducators != null)
                {
                    if (gvAllocatedEducators.Rows.Count > 0)
                    {
                        index = 0;
                        foreach (GridViewRow gvr in gvAllocatedEducators.Rows)
                        {
                            CheckBox RowLevelCheckBox = (CheckBox)gvr.FindControl("RowLevelCheckBox");
                            if (RowLevelCheckBox.Checked)
                            {
                                contactId = Int32.Parse(gvAllocatedEducators.DataKeys[index].Value.ToString());
                                oEducatorC.Delete(contactId);
                            }
                            index++;
                        }
                    }
                }
            }
        }

        this.gvAvailableEducators.DataBind();
        this.gvVenues.DataBind();
    }
    protected void gvAvailableEducators_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void ddUnitFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddCampusFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddSemesterFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
    protected void ddYearFilter_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.gvPlacementTypes.SelectedIndex = 0;
    }
}
